package cn.iocoder.yudao.module.diy.controller.admin.config;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.diy.pojo.LoginConfigVo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.successAdmin;

import cn.iocoder.yudao.module.diy.controller.admin.config.vo.*;
import cn.iocoder.yudao.module.diy.dal.dataobject.config.DiyConfigDO;
import cn.iocoder.yudao.module.diy.service.config.DiyConfigService;

@Tag(name = "管理后台 - 装修配置")
@RestController
@RequestMapping("/diy/config")
@Validated
public class DiyConfigController {

    @Resource
    private DiyConfigService configService;

    @PostMapping("/create")
    @Operation(summary = "创建装修配置")
    @PreAuthorize("@ss.hasPermission('diy:config:create')")
    public CommonResult<Long> createConfig(@Valid @RequestBody ConfigSaveReqVO createReqVO) {
        return success(configService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新装修配置")
    @PreAuthorize("@ss.hasPermission('diy:config:update')")
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody ConfigSaveReqVO updateReqVO) {
        configService.updateConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除装修配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('diy:config:delete')")
    public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) {
        configService.deleteConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得装修配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('diy:config:query')")
    public CommonResult<ConfigRespVO> getConfig(@RequestParam("id") Long id) {
        DiyConfigDO config = configService.getConfig(id);
        return success(BeanUtils.toBean(config, ConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得装修配置分页")
    @PreAuthorize("@ss.hasPermission('diy:config:query')")
    public CommonResult<PageResult<ConfigRespVO>> getConfigPage(@Valid ConfigPageReqVO pageReqVO) {
        PageResult<DiyConfigDO> pageResult = configService.getConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出装修配置 Excel")
    @PreAuthorize("@ss.hasPermission('diy:config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigExcel(@Valid ConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DiyConfigDO> list = configService.getConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "装修配置.xls", "数据", ConfigRespVO.class,
                        BeanUtils.toBean(list, ConfigRespVO.class));
    }
    /**
     *获取注册与登录设置
     * @return
     */
    @GetMapping("/member/login")
    public CommonResult<LoginConfigVo> getLoginConfig() {
        return successAdmin(JSONUtil.toBean(configService.getConfigValue("LOGIN"), LoginConfigVo.class));
    }
    /**
     * 设置注册与登录设置
     * @param configParam
     * @return
     */
    @PostMapping("/member/login")
    public CommonResult setLoginConfig(@Validated @RequestBody LoginConfigParam configParam) {
        if(null==configParam.getSiteId()){
            configParam.setSiteId(TenantContextHolder.getTenantId());
        }
        configService.setConfig(TenantContextHolder.getTenantId(),0l,configParam.getSiteId(),"LOGIN", JSONUtil.parse(configParam));
        return successAdmin(true);
    }

    /**
     * 获取商品分类配置
     *
     * @return
     */
    @GetMapping("/category")
    public CommonResult<GoodsCategoryConfigVo> getGoodsCategoryConfig() {
        return successAdmin(JSONUtil.toBean(configService.getConfigValue("GOODS_CATEGORY_CONFIG"), GoodsCategoryConfigVo.class));
    }

    /**
     * 设置商品分类配置
     *
     * @return
     */
    @PostMapping("/category")
    public CommonResult<Object> setGoodsCategoryConfig(@RequestBody JSONObject data) {
        configService.setConfig(TenantContextHolder.getTenantId(),0l,TenantContextHolder.getTenantId(),"GOODS_CATEGORY_CONFIG", data);
        return successAdmin(true);
    }



}